import { Init, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { MemberUserInfoEntity } from '../entity/info';
import { OrderInfoEntity } from "../../order/entity/info"
import { getWeekDates, updateTime } from '../../../comm/utils'

/**
 * member模块-用户信息
 */
@Provide()
export class MemberUserInfoService extends BaseService {
  @InjectEntityModel(MemberUserInfoEntity)
  memberUserInfoEntity: Repository<MemberUserInfoEntity>;

  @InjectEntityModel(OrderInfoEntity)
  orderInfoEntity: Repository<OrderInfoEntity>;


  @Init()
  async init() {
    await super.init();
    this.setEntity(this.memberUserInfoEntity);
  }

  async page(param: any) {
    console.log(param, 'param')
    let name = param.keyWord ? ' where username LIKE "%' + param.keyWord + '%" ' : ''
    console.log(name, 'name')
    let result = this.sqlRenderPage("select * from member_user_info " + name + " ORDER BY id " + param.sort, param, false);
    // 你自己的业务逻辑
    return result;
  }

  async orderTotal() {
    // 获取本周日期范围
    let thisWeek = getWeekDates('this');
    // 获取昨天
    let lastDay = getWeekDates('lastDay');
    // 获取上一周日期范围
    let lastWeek = getWeekDates('previous');

    let datares = await this.orderInfoEntity.createQueryBuilder().where("status >= :status", { status: 0 }).getCount();

    // 获取上一周数据
    let lastData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(lastWeek.start, 1), endDate: updateTime(lastWeek.end, 2) }).getCount()
    console.log(lastData, 'lastData上一周数据')

    // 获取本周数据
    let currentData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(thisWeek.start, 1), endDate: updateTime(thisWeek.end, 2) }).getCount();

    // 获取昨天的日期
    let yesterday = new Date();
    let yesterdayTime = yesterday.setDate(yesterday.getDate() - 1);

    // 获取昨天数据
    let yesterdayData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(new Date(yesterdayTime), 1), endDate: updateTime(new Date(yesterdayTime), 2) }).getCount()

    // 获取今天数据
    let currentDayData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(new Date(), 1), endDate: updateTime(new Date(), 2) }).getCount()
    return {
      orderTotal: datares,
      weekly: Math.floor(((currentData - lastData) / lastData)) * 100,
      yesterday: Math.floor(((currentDayData - yesterdayData) / yesterdayData)) * 100,
      currentDay: (currentDayData - yesterdayData)
    }
  }

  // 总销售额
  async totalSalesVolume() {
    // 获取本周日期范围
    let thisWeek = getWeekDates('this');
    // 获取昨天
    let lastDay = getWeekDates('lastDay');
    // 获取上一周日期范围
    let lastWeek = getWeekDates('previous');
    let data = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 }).select("SUM(totalPrice)", "totalPrice")
      .getRawOne();

    // 获取上一周数据
    let lastData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(lastWeek.start, 1), endDate: updateTime(lastWeek.end, 2) }).select("SUM(totalPrice)", "totalPrice")
      .getRawOne();

    // 获取本周数据
    let currentData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(thisWeek.start, 1), endDate: updateTime(thisWeek.end, 2) }).select("SUM(totalPrice)", "totalPrice")
      .getRawOne();;


    // 获取昨天的日期
    let yesterday = new Date();
    let yesterdayTime = yesterday.setDate(yesterday.getDate() - 1);

    // 获取昨天数据
    let yesterdayData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(new Date(yesterdayTime), 1), endDate: updateTime(new Date(yesterdayTime), 2) }).select("SUM(totalPrice)", "totalPrice")
      .getRawOne();

    // 获取今天数据
    let currentDayData = await this.orderInfoEntity.createQueryBuilder().where("status = :status", { status: 1 })
      .andWhere("orderTime BETWEEN :startDate AND :endDate", { startDate: updateTime(new Date(), 1), endDate: updateTime(new Date(), 2) }).select("SUM(totalPrice)", "totalPrice")
      .getRawOne();

    return {
      totalPrice: data.totalPrice,
      weekly: Math.floor(((Number(currentData.totalPrice) - Number(lastData.totalPrice)) / Number(lastData.totalPrice))) * 100,
      dayly: Math.floor(((Number(currentDayData.totalPrice) - Number(yesterdayData.totalPrice)) / Number(yesterdayData.totalPrice))) * 100,
    }
  }
}
